package com.x9space.framework.elastic.selector;



import com.x9space.framework.elastic.annotation.QueryBuilderMark;
import com.x9space.framework.elastic.handler.QueryHandler;

import java.lang.annotation.Annotation;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.ServiceLoader;

public class DefaultQueryHandleSelector implements QueryHandleSelector{


    private static Map<String, QueryHandler> handlerMap = new HashMap<>();

    static {

        ServiceLoader<QueryHandler> load =ServiceLoader.load(QueryHandler.class);
        Iterator<QueryHandler> iterator = load.iterator();
        while (iterator.hasNext()){
            QueryHandler next = iterator.next();
            String name = next.getName();
            handlerMap.put(name,next);
        }
    }

    @Override
    public QueryHandler doSelect(Annotation annotation) {
        if (annotation.annotationType().getAnnotation(QueryBuilderMark.class)==null){
            return null;
        }
        String simpleName = annotation.annotationType().getSimpleName();

        return handlerMap.get(simpleName);
    }
}
